package com.example.hello.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.hello.common.Result;
import com.example.hello.dto.EmployeeAddDTO;
import com.example.hello.dto.EmployeeQueryDTO;
import com.example.hello.dto.EmployeeUpdateDTO;
import com.example.hello.entity.Employee;
import com.example.hello.entity.WorkExperience;
import com.example.hello.service.EmployeeService;
import com.example.hello.vo.DeleteResultVO;
import com.example.hello.vo.EmployeeVO;
import com.example.hello.vo.EmployeeCreateVO;
import com.example.hello.vo.PageResultVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/employees")
@RequiredArgsConstructor
@Slf4j
public class EmployeeController {
    
    private final EmployeeService employeeService;

    @GetMapping
    public Result<PageResultVO<EmployeeVO>> list(EmployeeQueryDTO queryDTO) {
        try {
            IPage<Employee> page = employeeService.listEmployees(queryDTO);
            List<EmployeeVO> voList = page.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
            return Result.success(PageResultVO.of(page.getTotal(), voList));
        } catch (Exception e) {
            log.error("查询员工列表失败", e);
            throw e;
        }
    }

    @PostMapping
    public Result<EmployeeCreateVO> add(@Valid @RequestBody EmployeeAddDTO addDTO) {
        Employee employee = employeeService.addEmployee(addDTO);
        return Result.success(
            "员工创建成功",
            EmployeeCreateVO.of(employee.getId(), null, employee.getUpdateTime())
        );
    }

    @PutMapping("/{id}")
    public Result<?> update(
            @PathVariable Integer id,
            @Valid @RequestBody EmployeeUpdateDTO updateDTO) {
        employeeService.updateEmployee(id, updateDTO);
        return Result.success("员工更新成功");
    }

    @DeleteMapping
    public Result<DeleteResultVO> delete(@RequestBody Map<String, List<Integer>> payload) {
        List<Integer> ids = payload.get("ids");
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择要删除的员工");
        }
        
        int experienceCount = employeeService.deleteEmployees(ids);
        return Result.success(
            "成功删除" + ids.size() + "条记录", 
            DeleteResultVO.of(ids, experienceCount)
        );
    }

    private EmployeeVO convertToVO(Employee employee) {
        EmployeeVO vo = new EmployeeVO();
        vo.setId(employee.getId());
        vo.setUsername(employee.getUsername());
        vo.setName(employee.getName());
        vo.setGender(employee.getGender());
        vo.setPhoneNumber(employee.getPhoneNumber());
        vo.setDepartmentId(employee.getDepartmentId());
        vo.setPositionId(employee.getPositionId());
        vo.setAvatar(employee.getAvatar());
        vo.setJoinDate(employee.getJoinDate());
        vo.setCreateTime(employee.getCreateTime());
        vo.setUpdateTime(employee.getUpdateTime());
        
        if (employee.getDepartment() != null) {
            vo.setDepartmentName(employee.getDepartment().getDepartmentName());
        }
        if (employee.getPosition() != null) {
            vo.setPositionName(employee.getPosition().getPositionName());
        }
        
        return vo;
    }
} 